using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.Page
{
	public class SetBackgroundPage : BasePage
	{
		private const string TAG = "SetBackgroundPage";
		private EventWatcher m_GetPackageListOK;
		private EventWatcher m_GetBigImageOK;
		private EventWatcher m_GetBigImageErr;
		private string mUserName;
		private bool isSetDefaultBackground;
		private bool isDownloading;
		private List<SetBackgroundItem> allItems;
		private SetBackgroundItem mLastClickItem;
		private bool isOpenTask;
		private PhotoChooserTask photoChooserTask;
		private CameraCaptureTask photoCameraCapture;
		internal Grid LayoutRoot;
		internal ScrollViewer scrollViewer;
		internal StackPanel ContentPanel;
		internal WrapPanel WrapPanel;
		internal Border imgSetDefault;
		internal TextBlock txtSetDefault;
		internal Button btnSelectPhoto;
		internal Button btnTakePhoto;
		internal Button btnApplyToAll;
		private bool _contentLoaded;
		public SetBackgroundPage()
		{
			this.InitializeComponent();
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && object_0.Length > 0)
			{
				this.mUserName = (object_0[0] as string);
				this.m_GetPackageListOK = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventGetPackageListOK));
				this.m_GetBigImageOK = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventGetBigImageOK));
				this.m_GetBigImageErr = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventGetBigImageErr));
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.m_GetPackageListOK);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_SUCCESS, this.m_GetBigImageOK);
				EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, this.m_GetBigImageErr);
				this.isSetDefaultBackground = (this.mUserName == "DEFAULT_BACKGROUND_USER_NAME");
				if (this.isSetDefaultBackground)
				{
					this.btnApplyToAll.set_Visibility(0);
				}
				PacageDataList pacageDataList = PackageDataMgr.getPacageDataList();
				if (pacageDataList != null)
				{
					this.initItems(pacageDataList);
				}
				if (R.isEnglish())
				{
					this.txtSetDefault.set_Width(143.0);
				}
				return;
			}
			base.GoBack();
		}
		protected override void OnPageOut()
		{
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_GETPACKAGE_SUCCESS, this.m_GetPackageListOK);
			this.m_GetPackageListOK = null;
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_SUCCESS, this.m_GetBigImageOK);
			this.m_GetBigImageOK = null;
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, this.m_GetBigImageErr);
			this.m_GetBigImageErr = null;
		}
		private void onEventGetPackageListOK(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			PacageDataList pacageDataList = evtArgs.mObject as PacageDataList;
			if (pacageDataList != null)
			{
				this.initItems(pacageDataList);
			}
		}
		private void onEventGetBigImageOK(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			object[] array = evtArgs.mObject as object[];
			string text = array[0] as string;
			int imgId = (int)array[1];
			if (text == null)
			{
				this.onEventGetBigImageErr(null, null);
				return;
			}
			this.setServerBackground(text, imgId);
		}
		private void onEventGetBigImageErr(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			this.isDownloading = false;
			MessageBoxManager.ShowToast(strings.ImageViewDownError, 2500, null, false);
			if (this.mLastClickItem != null)
			{
				this.mLastClickItem.txtDownload.Text = strings.ChatBackground_ReadyForDownload;
			}
		}
		private void initItems(PacageDataList pacageDataList)
		{
			this.allItems = new List<SetBackgroundItem>();
			if (pacageDataList != null && pacageDataList.dataList != null)
			{
				string backGroundPath = ConversationMgr.getBackGroundPath(this.mUserName);
				using (List<PacageData>.Enumerator enumerator = pacageDataList.dataList.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						PacageData current = enumerator.get_Current();
						SetBackgroundItem setBackgroundItem = new SetBackgroundItem(current);
						this.setItemAttrs(setBackgroundItem);
						setBackgroundItem.imgSample.set_Source(ImageCacheMgr.loadFromFile(current.thumFileName));
						setBackgroundItem.LayoutItem.add_Click(new RoutedEventHandler(this.image_Click));
						if (backGroundPath == PackageDataMgr.getPackBigFileName(current))
						{
							setBackgroundItem.SetSelected(true);
						}
						else
						{
							setBackgroundItem.SetSelected(false);
						}
						this.allItems.Add(setBackgroundItem);
						this.WrapPanel.get_Children().Add(setBackgroundItem);
					}
				}
				return;
			}
		}
		private void setItemAttrs(SetBackgroundItem item)
		{
			item.set_Margin(new Thickness(0.0, 7.0, 7.0, 0.0));
		}
		private void deSelectAll()
		{
			if (this.allItems == null)
			{
				return;
			}
			using (List<SetBackgroundItem>.Enumerator enumerator = this.allItems.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SetBackgroundItem current = enumerator.get_Current();
					current.SetSelected(false);
				}
			}
		}
		private void image_Click(object sender, RoutedEventArgs e)
		{
			if (this.isDownloading)
			{
				return;
			}
			if (sender == this.imgSetDefault)
			{
				ConversationMgr.clearBackGround(this.mUserName, true);
				EventCenter.postEvent(EventConst.ON_CHAT_BACKGROUND_CHANGED, null, null);
				if (this.isSetDefaultBackground)
				{
					this.deSelectAll();
					MessageBoxManager.ShowToast(strings.ChatBackground_Set_OK, 2500, null, false);
				}
				else
				{
					base.GoBack();
				}
				ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_BACKGROUND_USED, "-2");
				return;
			}
			Button button = sender as Button;
			if (button == null)
			{
				return;
			}
			this.mLastClickItem = (button.get_DataContext() as SetBackgroundItem);
			if (this.mLastClickItem == null)
			{
				return;
			}
			string bigFile = PackageDataMgr.getBigFile(this.mLastClickItem.mPacageData);
			if (bigFile != null)
			{
				this.setServerBackground(bigFile, this.mLastClickItem.mPacageData.nId);
				return;
			}
			this.mLastClickItem.txtDownload.Text = strings.ChatBackground_Downloading + "...";
			this.isDownloading = true;
		}
		private void setServerBackground(string bigImgPath, int imgId)
		{
			if (bigImgPath == null)
			{
				return;
			}
			ConversationMgr.clearBackGround(this.mUserName, true);
			ConversationMgr.setBackGroundSvr(this.mUserName, bigImgPath);
			this.deSelectAll();
			if (this.mLastClickItem != null)
			{
				this.mLastClickItem.SetSelected(true);
			}
			this.isDownloading = false;
			EventCenter.postEvent(EventConst.ON_CHAT_BACKGROUND_CHANGED, null, null);
			ServiceCenter.statReportMgr.addStatItem(enKVStatKey.MM_BACKGROUND_USED, imgId.ToString());
			if (!this.isSetDefaultBackground)
			{
				base.GoBack();
			}
		}
		private void btnButton_Click(object sender, RoutedEventArgs e)
		{
			if (this.isDownloading)
			{
				return;
			}
			if (sender == this.btnSelectPhoto)
			{
				this.chooserPicture(false);
				return;
			}
			if (sender == this.btnTakePhoto)
			{
				this.takePhoto();
				return;
			}
			if (sender == this.btnApplyToAll)
			{
				ConversationMgr.clearAllBackGround(true);
				MessageBoxManager.ShowToast(strings.ChatBackground_Set_OK, 2500, null, false);
			}
		}
		public void SelectPicDlgReturn(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				this.takePhoto();
				return;
			case 1:
				this.chooserPicture(false);
				return;
			default:
				return;
			}
		}
		private void chooserPicture(bool isShowCamera)
		{
			if (this.isOpenTask)
			{
				return;
			}
			this.isOpenTask = true;
			this.photoChooserTask = new PhotoChooserTask();
			if (this.photoChooserTask == null)
			{
				return;
			}
			this.photoChooserTask.add_Completed(new EventHandler<PhotoResult>(this.chooserPicture_Completed));
			this.photoChooserTask.set_PixelHeight(800);
			this.photoChooserTask.set_PixelWidth(480);
			this.photoChooserTask.set_ShowCamera(isShowCamera);
			this.photoChooserTask.Show();
		}
		private void chooserPicture_Completed(object sender, PhotoResult photoResult_0)
		{
			this.isOpenTask = false;
			if (photoResult_0.get_Error() != null)
			{
				MMMessageBox.Show(strings.Set_Personal_CanNotLoadImages, true, "确定");
				return;
			}
			if (photoResult_0.get_TaskResult() == 1)
			{
				this.saveBackgroundImage(photoResult_0.get_ChosenPhoto());
			}
		}
		private void takePhoto()
		{
			if (this.isOpenTask)
			{
				return;
			}
			this.isOpenTask = true;
			this.photoCameraCapture = new CameraCaptureTask();
			if (this.photoCameraCapture == null)
			{
				return;
			}
			this.photoCameraCapture.add_Completed(new EventHandler<PhotoResult>(this.takePhoto_Completed));
			this.photoCameraCapture.Show();
		}
		private void takePhoto_Completed(object sender, PhotoResult photoResult_0)
		{
			this.isOpenTask = false;
			try
			{
				if (photoResult_0.get_TaskResult() == 1)
				{
					int num = 0;
					for (int i = 0; i < 3; i++)
					{
						photoResult_0.get_ChosenPhoto().Seek((long)(54 + i), 0);
						num = photoResult_0.get_ChosenPhoto().ReadByte();
						if (num != 0)
						{
							break;
						}
					}
					if (num != 6)
					{
						if (num != 8)
						{
							this.saveBackgroundImage(photoResult_0.get_ChosenPhoto());
							goto IL_152;
						}
					}
					BitmapImage bitmapImage = new BitmapImage();
					bitmapImage.SetSource(photoResult_0.get_ChosenPhoto());
					Image image = new Image();
					image.set_Height((double)(2 * bitmapImage.get_PixelWidth()));
					image.set_Width((double)(2 * bitmapImage.get_PixelWidth()));
					image.set_Source(bitmapImage);
					RotateTransform rotateTransform = new RotateTransform();
					rotateTransform.set_CenterX(image.get_Width() / 2.0);
					rotateTransform.set_CenterY(image.get_Height() / 2.0);
					rotateTransform.set_Angle(90.0);
					WriteableBitmap source = new WriteableBitmap(image, rotateTransform);
					Image image2 = new Image();
					image2.set_Width((double)bitmapImage.get_PixelHeight());
					image2.set_Height((double)bitmapImage.get_PixelWidth());
					image2.set_Source(source);
					image2.set_Stretch(3);
					WriteableBitmap writeableBitmap = new WriteableBitmap(image2, null);
					MemoryStream memoryStream = new MemoryStream();
					Extensions.SaveJpeg(writeableBitmap, memoryStream, (int)image2.get_Width(), (int)image2.get_Height(), 0, 100);
					this.saveBackgroundImage(memoryStream);
				}
				IL_152:;
			}
			catch (Exception ex)
			{
				Log.smethod_1("SetBackgroundPage", ex.get_Message() + "\n stackTrace:\n" + ex.get_StackTrace());
			}
			this.photoCameraCapture = null;
		}
		private void saveBackgroundImage(Stream imgStream)
		{
			if (imgStream == null || imgStream.get_Length() <= 0L)
			{
				return;
			}
			imgStream.Seek(0L, 0);
			ConversationMgr.clearBackGround(this.mUserName, true);
			ConversationMgr.setBackGround(this.mUserName, imgStream);
			EventCenter.postEvent(EventConst.ON_CHAT_BACKGROUND_CHANGED, null, null);
			if (this.isSetDefaultBackground)
			{
				this.deSelectAll();
				MessageBoxManager.ShowToast(strings.ChatBackground_Set_OK, 2500, null, false);
				return;
			}
			base.get_Dispatcher().BeginInvoke(delegate
			{
				base.GoBack();
			});
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/background/SetBackgroundPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.scrollViewer = (ScrollViewer)base.FindName("scrollViewer");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.WrapPanel = (WrapPanel)base.FindName("WrapPanel");
			this.imgSetDefault = (Border)base.FindName("imgSetDefault");
			this.txtSetDefault = (TextBlock)base.FindName("txtSetDefault");
			this.btnSelectPhoto = (Button)base.FindName("btnSelectPhoto");
			this.btnTakePhoto = (Button)base.FindName("btnTakePhoto");
			this.btnApplyToAll = (Button)base.FindName("btnApplyToAll");
		}
	}
}
